package com.norbitltd.guavaleaves.java.io;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import com.google.common.base.Predicate;

public class FilePredicates {
	
	/**
	 * @see java.io.File.canExecute()
	 * @return
	 */
	public static Predicate<File> canExecute() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.canExecute();
			}
		};
	}
	
	/**
	 * @see java.io.File.canRead()
	 * @return
	 */
	public static Predicate<File> canRead() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.canRead();
			}
		};
	}
	
	/**
	 * @see java.io.File.canWrite()
	 * @return
	 */
	public static Predicate<File> canWrite() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.canWrite();
			}
		};
	}
	

	public static Predicate<File> exists() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.exists();
			}
		};
	}
	
	/**
	 * @see java.io.File.isAbsolute()
	 * @return
	 */
	public static Predicate<File> isAbsolute() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.isAbsolute();
			}
		};
	}
	
	/**
	 * @see java.io.File.isDirectory()
	 * @return
	 */
	public static Predicate<File> isDirectory() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.isDirectory();
			}
		};
	}
	
	/**
	 * @see java.io.File.isFile()
	 * @return
	 */
	public static Predicate<File> isFile() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.isFile();
			}
		};
	}
	
	/**
	 * @see java.io.File.isHidden()
	 * @return
	 */
	public static Predicate<File> isHidden() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return input.isHidden();
			}
		};
	}
	
	public static Predicate<File> isSymlink() {
		return new Predicate<File>() {
			public boolean apply(File input) {
				try {
					return FileUtils.isSymlink(input);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	public static Predicate<File> isFileNewer(final Date date) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return FileUtils.isFileNewer(input, date);
			}
		};
	}
	
	public static Predicate<File> isFileNewer(final File reference) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return FileUtils.isFileNewer(input, reference);
			}
		};
	}
	
	public static Predicate<File> isFileNewer(final long timeInMilis) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return FileUtils.isFileNewer(input, timeInMilis);
			}
		};
	}
	
	public static Predicate<File> isFileOlder(final Date date) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return FileUtils.isFileOlder(input, date);
			}
		};
	}
	
	public static Predicate<File> isFileOlder(final File reference) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return FileUtils.isFileOlder(input, reference);
			}
		};
	}
	
	public static Predicate<File> isFileOlder(final long timeInMilis) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				return FileUtils.isFileOlder(input, timeInMilis);
			}
		};
	}
	
	public static Predicate<File> contentEquals(final File file2) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				try {
					return FileUtils.contentEquals(input, file2);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
				
			}
		};
	}
	
	public static Predicate<File> contentEqualsIgnoreEOL(final File file2, final String charsetName) {
		return new Predicate<File>() {
			public boolean apply(File input) {
				try {
					return FileUtils.contentEqualsIgnoreEOL(input, file2, charsetName);	
				} catch(Exception e) {
					throw new RuntimeException(e);
				}
				
			}
		};
	}


}
